package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.model.SystemProperties;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.LiveGoodsService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 直播带货h5
 */
@RequestMapping("/h5/liveGoods")
@RestController
public class LiveGoodsh5Controller {

    @Autowired
    private LiveGoodsService liveGoodsService;

    @Autowired
    private RequestFilter requestFilter;

    /**
     * 直播间信息
     *
     * @param : studioId  直播间Id
     */
    @PostMapping("/getStudioInfo")
    public Object getStudioInfo(@RequestBody Map<String, Object> params) {
        return liveGoodsService.getStudioInfo(params);
    }

    /**
     * 商品列表
     *
     * @param : goodsds 所有商品id
     */
    @PostMapping("/getGoodslist")
    public Object getGoodsList(@RequestBody Map<String, Object> params) {
        return liveGoodsService.geth5LiveGoodsList(params);
    }

    /**
     * 直播间列表
     */
    @GetMapping("/getStudiolist")
    public Object getStudiolist(HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orgId", orgId);
        map.put("customerId", customerId);
        return liveGoodsService.getStudiolist(map);
    }

    /**
     * 判断商家认证
     */
    @PostMapping("/getAuthenticateState")
    public String getAuthenticateState(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        params.put("orgId", orgId);
        params.put("customerId", customerId);
        return liveGoodsService.getAuthenticateState(params);
    }


    /**
     * 创建直播间
     */
    @PostMapping("/saveStudio")
    public Object saveStudio(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        params.put("orgId", orgId);
        params.put("customerId", customerId);
        params.put("type",2);
        return liveGoodsService.saveStudio(params);
    }

    /**
     * 获取管理员账号详情
     */
    @GetMapping("/getH5StudioAdminInfo")
    public Object getH5StudioAdminInfo(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        params.put("orgId", orgId);
        params.put("customerId", customerId);
        return liveGoodsService.getH5StudioAdminInfo(params);
    }


    /**
     * 获取直播间详情
     */
    @GetMapping("/getH5StudioInfo")
    public Object getH5StudioInfo(String studioId) {
        Map<String, Object> params = new HashMap<>();
        params.put("studioId",studioId);
        return liveGoodsService.getH5StudioInfo(params);
    }


    /**
     * 开通直播间 请求云观开通账号
     *
     * @return
     */
    @GetMapping("/openingstream")
    public void openingStream(HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        // 判断是否联盟商家
        int sale = liveGoodsService.getSaleByCustomerId(customerId);
        if(sale<1){
            throw GlobalException.build("请先开通联盟商家！");
        }
        String orgId = wxUser.getOrgId();
        // 手机号
        String bindPhone = liveGoodsService.getBindPhoneByCustomerId(customerId);
        // 频道名称
        String nickName = StringUtils.isEmpty(wxUser.getNickname()) ? "手机尾号" + bindPhone.substring(bindPhone.length() - 4) + "用户" : wxUser.getNickname();
        // 开通 直播间
        Map<String, Object> params = new HashMap<>();
        params.put("customerId", customerId);
        params.put("orgId", orgId);
        params.put("bindPhone", "mbl"+bindPhone);
        params.put("nickName", nickName);
        //
        params.put("logoUrl", "https://incloud.linker.cc/StepLink/202004/10/14/cXtzK2rdM202004101430.png");
        params.put("iconUrl", "https://incloud.linker.cc/StepLink/202004/10/14/1SLqvVhb3AuZCdscY5vgfrLos2020041014147.png");
        //类型1机构2商家
        params.put("type", 2);
        liveGoodsService.openingStream(params);
    }
}
